package com.lyshop.guiyingshanding.activitys;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;

import com.lyshop.guiyingshanding.R;
import com.lyshop.guiyingshanding.adapters.InvestSchoolAdapter;
import com.lyshop.guiyingshanding.base.BaseActivity;
import com.lyshop.guiyingshanding.bean.InvestSchoolGroupBean;
import com.lyshop.guiyingshanding.bean.InvestSchoolItemBean;
import com.lyshop.guiyingshanding.net.ActionCallbackListener;
import com.lyshop.guiyingshanding.utils.RouteUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mr on 2018/5/10.
 */

public class InvestSchoolActivity extends BaseActivity implements InvestSchoolAdapter.InvestSchoolListener, OnRefreshListener, RadioGroup.OnCheckedChangeListener {
    private RadioGroup rg_group;
    private SmartRefreshLayout refresh_layout;
    private ListView lv_list;
    private InvestSchoolAdapter mAdapter;
    private List<InvestSchoolGroupBean> mList = new ArrayList<>();
    private int mCheckIndex = 1;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAdapter = new InvestSchoolAdapter(this, this);
        mAdapter.setShowImageOnly(true);
        lv_list.setAdapter(mAdapter);
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_invest_school;
    }

    @Override
    protected void findViews() {
        rg_group = findViewById(R.id.rg_group);
        refresh_layout = findViewById(R.id.refresh_layout);
        lv_list =  findViewById(R.id.lv_list);
    }

    @Override
    protected void setListeners() {
        refresh_layout.setOnRefreshListener(this);
        rg_group.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClickItem(InvestSchoolItemBean item) {
        RouteUtil.toWebViewActivity((BaseActivity) mContext, item.getLink());
    }

    @Override
    protected void onResume() {
        super.onResume();
        onRefresh(null);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        mAppAction.getInvestSchoolList(new ActionCallbackListener<List<InvestSchoolGroupBean>>() {
            @Override
            public void onSuccess(List<InvestSchoolGroupBean> data) {
                mList.clear();
                mList.addAll(data);
                if (data.size() == 0) {
                    rg_group.setVisibility(View.GONE);
                    mAdapter.clear();
                } else if (data.size() == 1) {
                    rg_group.setVisibility(View.GONE);
                    mAdapter.setDataSource(data.get(0).getItem());
                } else {
                    ((RadioButton) rg_group.getChildAt(0)).setText(data.get(1).getTitle());
                    ((RadioButton) rg_group.getChildAt(1)).setText(data.get(0).getTitle());
//                    ((RadioButton) rg_group.getChildAt(2)).setText(data.get(2).getTitle());
                    rg_group.setVisibility(View.VISIBLE);
                    mAdapter.setDataSource(data.get(mCheckIndex).getItem());
                }
                refresh_layout.finishRefresh(1000);
            }

            @Override
            public void onFailure(String errorCode, String message) {
                refresh_layout.finishRefresh(1000);
            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.rb_left) {
            mCheckIndex = 1;
            mAdapter.setShowImageOnly(true);
        } else if(checkedId == R.id.rb_middle){
            mCheckIndex = 0;
            mAdapter.setShowImageOnly(false);
        }
        mAdapter.setDataSource(mList.get(mCheckIndex).getItem());
    }

}
